package lanou.a36ke_01.activity.newsactivity;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import lanou.a36ke_01.R;
import lanou.a36ke_01.activity.LogonActivity;
import lanou.a36ke_01.activity.newsactivity.newstool.NewsScrollView;
import lanou.a36ke_01.activity.newsactivity.newstool.PopupWindowActivity;
import lanou.a36ke_01.beanlists.newsbean.CollectionBean;
import lanou.a36ke_01.beanlists.newsbean.NativeBean;
import lanou.a36ke_01.beanlists.newsbean.NewsChildBean;
import lanou.a36ke_01.imagegetter.HtmlTextView;
import lanou.a36ke_01.tools.BaseActivity;
import lanou.a36ke_01.tools.DBTool;
import lanou.a36ke_01.tools.MyUser;
import lanou.a36ke_01.tools.NetTool;


public class NewsChildActivity extends BaseActivity implements NewsScrollView.OnScrollListener, View.OnClickListener {

    private String url = "https://rong.36kr.com/api/mobi/news/";
    private TextView author;
    private ImageView imageView;
    private TextView title;
    private String urlTwo = "https://rong.36kr.com/api/mobi/news/";
    private HtmlTextView contextTv;
    private ImageView popup;
    PopupWindowActivity menuWindow;
    private String urlAdd;
    private ScrollView scrollView;
    private ImageView back;
    private ImageView comment;
    private ImageView favorvite;
    private ImageView share;
    private ImageView more;
    private LinearLayout gone;
    private RelativeLayout rlayout;
    private NewsScrollView myScrollView;
    private int searchLayoutTop;
    private String urlAddTwo;
    private String name;
    private String bitmap;


    @Override
    protected int setLayout() {
        return R.layout.newschlid_activity;
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        gone = bindView(R.id.news_child_gone_linearlayout);
        rlayout = bindView(R.id.news_child_relative);
        back = bindView(R.id.news_child_back);
        comment = bindView(R.id.news_child_comment);
        favorvite = bindView(R.id.news_child_favorite);
        share = bindView(R.id.news_child_share);
        more = bindView(R.id.news_child_more);
        contextTv = (HtmlTextView) findViewById(R.id.news_child_context);
        title = bindView(R.id.news_child_title);
        author = bindView(R.id.news_child_text_author);
        imageView = bindView(R.id.news_child_image);
        popup = bindView(R.id.news_child_popup);
        myScrollView = bindView(R.id.news_child_scroll);
    }
    //popup的监听 当点popup其他地方的时候关闭popup
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            menuWindow.dismiss();
        }
    };

    @Override
    protected void initDate() {
        comment.setOnClickListener(this);
        back.setOnClickListener(this);
        favorvite.setOnClickListener(this);
        share.setOnClickListener(this);

        //popupWindow的点击事件
        popup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuWindow = new PopupWindowActivity(NewsChildActivity.this, itemsOnClick);
                //显示窗口
                menuWindow.showAsDropDown(NewsChildActivity.this.findViewById(R.id.news_child_hengxian));
                menuWindow.getFeedId(urlAdd);
            }
        });
        //自定义ScrollView的监听
        myScrollView.setOnScrollListener(this);

        Intent intent = getIntent();
        urlAdd = intent.getStringExtra("newsFeelId");

        mNetTool.getNetData(url + urlAdd, NewsChildBean.class, new NetTool.NetListener<NewsChildBean>() {
            @Override
            public void onSuccess(NewsChildBean newsChildBean) {
                name = newsChildBean.getData().getTitle();
                Log.d("NewsChildActivity" + "标题", name);
                bitmap = newsChildBean.getData().getUser().getAvatar();
                Log.d("NewsChildActivity" + "头像", bitmap);

                title.setText(newsChildBean.getData().getTitle());
                mNetTool.getImage(newsChildBean.getData().getUser().getAvatar(), imageView);
                String html = newsChildBean.getData().getContent();
                html.trim();
                html = html.replace("<p >", "<p>");
                html = html.replace("<blockquote >", "");
                html = html.replace("</blockquote>", "");
                html = html.replace("<br/>", "");
                html = html.replace("<ul  class=\" list-paddingleft-2\">", "");
                html = html.replace("</ul>", "");
                html = html.replace("</li>", "");
                html = html.replace("<li>", "");
                Log.d("NewsChildActivity", html);
                contextTv.setHtmlFromString(html, false);
                author.setText(newsChildBean.getData().getUser().getName());

            }

            @Override
            public void onError(String errorMsg) {

            }
        });

        //pop跳转时传过来的PostId
        Intent popDetails = getIntent();
        urlAddTwo = popDetails.getStringExtra("newsPostId");
        mNetTool.getNetData(urlTwo + urlAddTwo, NewsChildBean.class, new NetTool.NetListener<NewsChildBean>() {
            @Override
            public void onSuccess(NewsChildBean newsChildBean) {
                title.setText(newsChildBean.getData().getTitle());
                mNetTool.getImage(newsChildBean.getData().getUser().getAvatar(), imageView);
                String html = newsChildBean.getData().getContent();
                html.trim();
                html = html.replace("<p >", "<p>");
                html = html.replace("<blockquote >", "");
                html = html.replace("</blockquote>", "");
                html = html.replace("<br/>", "");
                html = html.replace("<ul  class=\" list-paddingleft-2\">", "");
                html = html.replace("</ul>", "");
                html = html.replace("</li>", "");
                html = html.replace("<li>", "");
                Log.d("NewsChildActivity", html);
                contextTv.setHtmlFromString(html, false);
                author.setText(newsChildBean.getData().getUser().getName());
            }


            @Override
            public void onError(String errorMsg) {

            }
        });


    }

    //获取顶部位置
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            searchLayoutTop = rlayout.getBottom();
        }
    }


    //当ScrollView的长度大于屏幕的长度时 隐藏图标 否则显示
    @Override
    public void onScroll(int scrollY) {
        if (scrollY >= searchLayoutTop) {
            gone.setVisibility(View.GONE);

        } else {
            gone.setVisibility(View.VISIBLE);
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.news_child_back:
                finish();
                break;
            case R.id.news_child_comment:
                Intent intent = new Intent(this, CommentActivity.class);
                startActivity(intent);
                break;
            case R.id.news_child_favorite:


                NativeBean nativeBean = new NativeBean();
                DBTool dbTool = DBTool.getInstance();
                nativeBean.setTitle(name);
                nativeBean.setUrl(bitmap);
                Log.d("NewsChildActivity", bitmap);
                MyUser myUser1 = MyUser.getCurrentUser(MyUser.class);
                nativeBean.setUesName(myUser1.getUsername());
                dbTool.insertUser(nativeBean);


                //实现收藏的功能
                //缓存的用户对象 可以实现通过缓存对象进行登录
                MyUser bmobUser = MyUser.getCurrentUser(MyUser.class);
                if (bmobUser != null) {
                    MyUser myUser = MyUser.getCurrentUser(MyUser.class);
                    if (myUser != null) {
                        CollectionBean collectionBean = new CollectionBean();
                        String nameId = BmobUser.getCurrentUser().getUsername();
                        collectionBean.setTitle(name);
                        Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
//                        collectionBean.setPic(bitmap);
                        collectionBean.setUesrName(nameId);
                        Log.d("NewsChildActivity**", name + bitmap);
                        collectionBean.save(new SaveListener<String>() {
                            @Override
                            public void done(String s, BmobException e) {
                                if (e == null) {
                                    Log.d("NewsChildActivity", "数据创建成功" + s);
                                    Toast.makeText(NewsChildActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();
                                } else {
                                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                                }
                            }
                        });
                    } else {
                    }
                } else {
                    Intent collect = new Intent(this, LogonActivity.class);
                    Log.d("NewsChildActivity11", "没登录");
                    startActivity(collect);
                    Toast.makeText(this, "不登录你收藏个毛线啊!!!!!", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.news_child_share:
                showShare();
                break;
        }
    }

    private void showShare() {
        ShareSDK.initSDK(this);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        // 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        // oks.setTitle(getString(R.string.share));
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");
        // 启动分享GUI
        oks.show(this);
    }

}


//https://z.36kr.com/class 36kr了解股权投资